package org.apache.struts.ehr.medical_records.action;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.axis2.AxisFault;
import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.Query;
import org.apache.struts.ehr.medical_records.model.Ward;

import ca.uhn.hl7v2.HL7Exception;

import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateQRYA19;
import com.earogya.logic.medical_records.ParseReply;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SelectWardAction extends ActionSupport{
	
	private Ward ward;
	private String occupiedBeds;
	private Boolean isReply=false;
	private HttpServletRequest request;
	private Boolean isWardEmpty=false;
	
		
	public Boolean getIsWardEmpty() {
		return isWardEmpty;
	}

	public Boolean getIsReply() {
		return isReply;
	}


	public String getOccupiedBeds() {
		return occupiedBeds;
	}

	public Ward getWard() {
		return ward;
	}

	public void setWard(Ward ward) {
		this.ward = ward;
	}

	public String execute() throws HL7Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		Query query=new Query();
		query.setPatientId(String.valueOf(ward.getWardNo()));
		query.setSearch_type("occupied beds");
		CreateQRYA19 qry=new CreateQRYA19(query);
		String msg=qry.createMessage("CSTH");
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			try {
				String reply=c.sendMessage(msg);
				if(!reply.equals("No Reply")){
					ParseReply parse=new ParseReply();
					occupiedBeds=(String)parse.parseMessage(reply);
					if(occupiedBeds!=null){
						isReply=true;
					}
					else{
						isWardEmpty=true;
						isReply=true;
					}
				}
				System.out.println(reply);
			} catch (AxisFault e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		else{
			
		}
		return SUCCESS;
		
	}
	
}
